<template>
  <!-- 
  这边是旧版的号池，
  保留之前代码，
  防止来回修改
   -->
  <div class="wuliu">
    <div class="powertop">
      <router-link class="powertopli " to="/gongju/wuliu">物流轨迹查询</router-link>
      <router-link class="powertopli " to="/gongju/net" v-if="shopid == 304676 || shopid == 50481" >在网及余额查询</router-link>
      <router-link class="powertopli " to="/gongju/dingdan" v-if="shopid == 304676 || shopid == 50481" >订单重复查询</router-link>
      <router-link class="powertopli " to="/gongju/shanghao"  >自动上号号池管理</router-link>
      <router-link class="powertopli " to="/gongju/shanghaolianjie"  >自动上号链接管理</router-link>
      <router-link class="powertopli " to="/gongju/erci"  v-if="shopid == 304676 || shopid == 50481" >二次引导下单</router-link>
      <router-link class="powertopli " to="/gongju/shaixuandingdan" v-if="shopid == 304676 || shopid == 50481" >筛选订单</router-link>
    </div>
    <div v-loading='loading' style="padding: 20px;">

      <div class="kflexc">
        <el-button type="danger" class="t_dr_btn"  @click="logkai()">添加号池</el-button>
      </div>
      <el-table :data="list" border style="width: 100%; margin: 20px 0;" ref="multipleTable" >


        <el-table-column align='center' prop='name' label="号池"></el-table-column>
        <el-table-column align='center' prop='type_name' label="运营商"></el-table-column>
        <el-table-column align='center' prop='count_all' label="号码数量"></el-table-column>
        <el-table-column align='center' prop='sold_count' label="已售出数量"></el-table-column>
        <el-table-column align='center' prop='available' label="剩余号码"></el-table-column>
        <el-table-column align='center' prop='created_at' label="创建时间"></el-table-column>

        <el-table-column align='center' label="操作">
          <template slot-scope="scope">
            <el-button type="text" style="margin-left: 0;" size="mini" @click="logkai(scope.row)">编辑</el-button>
            <el-button type="text" style="margin-left: 0;" size="mini" @click="logkai2(scope.row)">管理号码</el-button>
            <el-button type="text" style="margin-left: 0;" size="mini" @click="del(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination style="text-align: right;" background layout="total,prev, pager, next" :current-page="all.page" @current-change="fanye" :page-size="all.page_size" :total="total"></el-pagination>
    </div>
    <el-dialog title="添加号池" center :visible.sync="log" width="440px"  :close-on-click-modal="false" :close-on-press-escape="false">

        <div class="kflexc" style="margin-top: 20px;">
          <el-select v-model="all2.type" style="width: 400px;" placeholder="请选择所属运营商">
            <el-option v-for="item in sanlist" :label="item.name" :key="item.id" :value="item.id"></el-option>
          </el-select>
        </div>
        <div class="kflexc" style="margin-top: 20px;">
          <el-input placeholder="请输入号池名称" style="width: 400px; " v-model="all2.name"></el-input>
        </div>

    	  <span slot="footer" class="dialog-footer">
    		<el-button type="info" round @click="log = false">取消</el-button>
    		<el-button type="danger" round @click="shangchuan">确定</el-button>
    	  </span>
    </el-dialog>
  </div>
</template>

<script>
  import axios from 'axios'
  import moment from "moment";
  import XLSX from 'xlsx';
  import {
      read,utils
    } from 'xlsx'

  export default {
    name: "wuliu",
    data() {
      return {
        sanlist: [
          {
        		name: "中国联通",
        		id: 1
        	},
        	{
        		name: "中国移动",
        		id: 2
        	},
        	{
        		name: "中国电信",
        		id: 3
        	},
          {
        		name: "中国广电",
        		id: 4
        	}
        ],
        all2:{
          name:"",
          // cate:3,
          type:"",
          pool_id:""
        },
        loading:false,
        list:"",
        all:{
          page:1,
          page_size:20,
          // cate:3
        },
        total:0,
        shopid:'',
        log:false
      }
    },
    created() {
      //店铺权限问题
			this.shopid = JSON.parse(localStorage.getItem('SHOP')).shop_id;
      this.getlist()
      //获取当前年月
      // this.getNowDate()
      // console.log(this.vsdate())


    },
    mounted() {

    },
    methods: {
      shangchuan:function(){
        axios.post('/api/plan-market/pool/add',this.all2)
          .then(response => {
            if (response.data.msg.code == 0) {
              this.$message.success(`操作成功`);
              this.getlist()
              this.log =false
            } else {
              this.$message.error(response.data.msg.info);
            }
          })
      },
      logkai2:function(item){
        this.$router.push('/gongju/shanghao/haoma?id='+item.id)
      },
      logkai:function(item){
        if(item){
          this.all2 = {
            name:item.name,
            // cate:item.cate,
            type:item.type,
            pool_id:item.id
          }
        }else{
          this.all2 = {
            name:"",
            // cate:3,
            type:"",
            pool_id:""
          }
        }
        console.log(this.all2)
        this.log = true
      },
      readExcel1(files) {
        return new Promise((res, rej) => {
          var that = this;
          if (files.length <= 0) {
            return;
          }
          if (!/\.(xls|xlsx)$/.test(files[0].name.toLowerCase())) {
            this.$Message.error('上传格式不正确，请上传xls或者xlsx格式');
            return;
          }
          const fileReader = new FileReader();
          fileReader.onload = (ev) => {
            try {
              const data = ev.target.result;
              const workbook = read(data, {
                type: 'binary'
              });
              // 取第一张表
              const wsname = workbook.SheetNames[0];
              // 生成json表格内容
              const ws = utils.sheet_to_json(workbook.Sheets[wsname]);
              console.log(ws);
              // 后续为自己对ws数据的处理
              res(ws)
            } catch (e) {
              return false;
              rej(e)
            }
          };
          fileReader.readAsBinaryString(files[0]);
        })

      },

      uploasd2(file, fileList) {
        var that = this
        let files = {
          0: file
        }
        let data = {
          pool_id:"",
          numbers:[]
        }

        let ff = this.readExcel1(files);
        ff.then((res) => {

          res.forEach(item => {
            let obj = {
              mobile: item['手机号码'],
              province: item['省'],
              city: item['市'],
            }
            data.numbers.push(obj)
          })

          axios.post('/api/plan-market/numbers/import',data)
            .then(response => {
              if (response.data.msg.code == 0) {
                this.$message.success(`导入成功`);
                this.getlist()
              } else {
                this.$message.error(response.data.msg.info);
              }
            })
        })
      },

      //统计

      search: function() {
        this.all.page = 1
        this.getlist()
      },
      //翻页
      fanye: function(page) {
        this.loading = true
        this.all.page = page
        this.getlist()
      },
      //获取列表
      getlist: function() {
        axios.get('/api/plan-market/pool/list',{params:this.all})
          .then(response => {
            if (response.data.msg.code == 0) {
              this.list = response.data.data.data
              this.total = response.data.data.total
              this.loading = false
            } else {
              this.$message.error(response.data.msg.info);
            }
          })
      },

      //下载模板
      xiazai: function() {
        const a = document.createElement('a');
        a.setAttribute('download', '订单重复查询.xlsx');
        a.setAttribute('href',location.origin + location.pathname + 'static/dingdanchongfu.xlsx');
        a.click();
      },

      //真下载
      xiazai2: function(dizhi) {
        var a = document.createElement('a');

        a.setAttribute('href',location.origin + '/' + dizhi);
        a.click();
      },

      del(row){
        this.$confirm('是否确认删除?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          axios.post('/api/plan-market/pool/delete',{
            pool_id:row.id
          })
            .then(response => {
              if (response.data.msg.code == 0) {
                this.$message.success(`操作成功`);
                this.getlist()
              } else {
                this.$message.error(response.data.msg.info);
              }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          });
        });
      }
    }
  }
</script>

<style lang="scss" scoped>
  .wuliu {
    .powertop {
    	display: flex;
    	background:#f9f9f9;
    }
    .powertopli {
    	flex-basis: 180px;
    	line-height: 50px;
    	border-bottom: 3px #F9F9F9 solid;
    	color: #727272;
    	border-right: 1px #F9F9F9 solid;
    	font-size: 16px;
    	text-align: center;
    }
    .router-link-active {
    	background: #FFFFFF;
    	border-bottom: 3px #2974FF solid;
    }
    /deep/ .el-pagination.is-background .btn-prev:disabled{ background: rgba(246, 246, 246, 0.3); border: 1px rgba(167, 167, 167, 0.3) solid;}
    /deep/ .el-pagination.is-background .btn-next:disabled{ background: rgba(246, 246, 246, 0.3); border: 1px rgba(167, 167, 167, 0.3) solid;}
    /deep/ .el-pagination.is-background .btn-next{ background: rgba(246, 246, 246,1); border: 1px rgba(167, 167, 167,1) solid;}
    /deep/ .el-pagination.is-background .btn-prev{ background: rgba(246, 246, 246,1); border: 1px rgba(167, 167, 167,1) solid;}
    /deep/ .el-pagination.is-background .el-pager li:not(.disabled).active{ background-color: #2974FF;}
    /deep/ .el-pagination.is-background .el-pager li:not(.disabled).active:hover {
        color: #fff;
    }
    /deep/ .el-pagination.is-background .el-pager li:not(.disabled):hover {
        color: #2974FF;
    }
    /deep/ .el-pager li{ border-radius: 14px; background: rgba(255, 255, 255, 1); border: 1px rgba(167, 167, 167, 0.3) solid;}
    /deep/ .btn-prev{ border-radius: 14px; padding: 0 10px;}
    /deep/ .btn-next{ border-radius: 14px; padding: 0 10px;}

    /deep/ .el-table th{ background: rgb(246, 246, 246);}
    /deep/ .el-input__inner{ line-height: 36px; height: 36px; font-size: 12px; background: rgb(247, 248, 250);  border: 0;}
    /deep/ .el-date-editor .el-range-input{ background: rgb(247, 248, 250); }
    /deep/ .el-button{ padding: 11px 23px !important;}
    /deep/ .el-button--mini{ padding: 6px 15px !important;}
    /deep/ .el-button--danger {
        color: #FFF;
        background-color: #2974FF;
        border-color: #2974FF;
    }
    .team {
    	background: #F9F9F9;
    }
  }
</style>
